
Page({

  data: {
    username:'',
    emailKey:'',
    newPassword:'',
    email:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  inputEmailKey:function(e){
    console.log(e.detail.value);
    this.setData({emailKey:e.detail.value});
  },
  inputEmail:function(e){
    console.log(e.detail.value);
    this.setData({email:e.detail.value});
  },
  inputNewPassword:function(e){
    console.log(e.detail.value);
    this.setData({newPassword:e.detail.value});
  },
  inputUsername:function(e){
    console.log(e.detail.value);
    this.setData({username:e.detail.value});
  },

  change:function(){
    console.log(this.data);
    wx.request({
      url: 'http://81.70.199.174:8081/user/editPwdByEmail',
      method:'POST',
      data: {
        emailKey: this.data.emailKey,
        newPassword: this.data.newPassword,
        username:this.data.username
      },
      success:(res)=>{
        console.log(res);
        if(res.data.code===200){
          wx.showToast({
            title: res.data.message,
            icon:'success',
            duration:2000
          });
          setTimeout(()=>{
            wx.navigateTo({
              url: '/pages/login/login',
            });
          },1000);
        }else if(res.data.code===1001){
          wx.showToast({
            title: res.data.message,
            icon:'error',
            duration:2000
          });
        }else if(res.data.code===1002){
          wx.showToast({
            title: res.data.message,
            icon:'error',
            duration:2000
          });
        }else if(res.data.code===1004){
          wx.showToast({
            title: res.data.message,
            icon:'error',
            duration:2000
          });
        }
      },
      fail:(err)=>{
        console.log(err);
        wx.showToast({
          title: res.data.message,
          icon:'error',
          duration:2000
        });
      }   
    })
  },
  getEmailKey:function(){
    if(!this.data.email) {
      wx.showToast({title: '请输入邮箱', icon: 'error'});
      return;
    }
    wx.request({
      url: 'http://81.70.199.174:8081/user/getEmail',
      method:'POST',
      data:{
        email: this.data.email,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      success:(res)=>{
        console.log(res);
        if(res.data.code===200){
          wx.showToast({title: '验证码已发送', icon:'success'});
          this.setData({emailKey:res.data.data});
        }else{
          wx.showToast({title: res.data.message || '发送失败', icon:'error'});
        }
      },
      fail:(err)=>{
        wx.showToast({title: '网络错误', icon:'error'});
      }
    })
  },
  onLoad(options) {

  },
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})